package com.ken.user.service.impl;
import java.io.File;

import cn.hutool.core.util.RandomUtil;
import com.ken.common.Enum.TemplateCode;
import com.ken.common.pojo.*;
import com.ken.common.pojo.Class;
import com.ken.common.result.Result;
import com.ken.common.utils.*;
import com.ken.common.vo.captchaVo;
import com.ken.fx.*;
import com.ken.user.dto.*;
import com.ken.user.mapper.userMapper;
import com.ken.user.service.userService;
import com.ken.user.vo.*;
import com.ken.util.WordDocumentUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class userServiceImpl implements userService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private userMapper userMapper;
    @Override
    public captchaVo getCaptcha() {
        captcha captcha = captchaUtil.getCaptcha();
        log.info("验证码是："+captcha);
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(key,captcha.getCode(),1, TimeUnit.MINUTES);
        captchaVo captchaVo = new captchaVo();
        captchaVo.setCodeKey(key);
        captchaVo.setCodeValue("data:image/png;base64,"+captcha.getCodeImage());
        return captchaVo;
    }

//    @Override
//    public Result loginWithPassword(loginWithPasswordDto loginWithPasswordDto) {
//        String codeValue = redisTemplate.opsForValue().get(loginWithPasswordDto.getCodeKey());
//        if (codeValue==null){
//            return Result.error("验证码过期");
//        }
//        if (!codeValue.equals(loginWithPasswordDto.getCode())) {
//            return Result.error("验证码错误");
//        }
//        student student = userMapper.selectByPhone(loginWithPasswordDto.getPhone());
//        if (student != null){
//            String input_password = DigestUtils.md5DigestAsHex(loginWithPasswordDto.getPassword().getBytes());
//            if (!input_password.equals(student.getPassword())){
//                return Result.error("密码错误");
//            }
//            // 密码正确生成JWT并且将用户信息存入local
//            AuthContextUtil.set(user);
//            String jwt = JWTUtil.createJWT(user);
//            return Result.success(jwt);
//        }else {
//            return Result.error("用户未注册");
//        }
//    }

    @Override
    public Result loginWithFace(loginWithFaceDto loginWithFaceDto) throws Exception {
        //先进行活体检测
        boolean antiSpoof = WebAntiSpoof.antiSpoof(loginWithFaceDto.getImgBase64(), loginWithFaceDto.getImgEncoder());
        if (!antiSpoof){
            return Result.error("未通过活体检测");
        }
        //通过活体检测进行人脸对比
        student student = userMapper.selectStudentByPhone(loginWithFaceDto.getPhone());
        if (student!=null){
            boolean compare = WebFaceCompare.faceCompare(imgUtil.downLoadImgToBase64(student.getFaceUrl()), student.getFaceEncoder(), loginWithFaceDto.getImgBase64(), loginWithFaceDto.getImgEncoder());
            if (compare){
                user user = new user(student.getSchool(),student.getStudent_id(),student.getName(),student.getPhone(),"0",student.getImg(),student.getDepartment(),"");
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("登录失败，对比不正确");
            }
        }else {
            teacher teacher = userMapper.selectTeacherByPhone(loginWithFaceDto.getPhone());
            if (teacher==null) return Result.error("用户不存在");
            boolean compare = WebFaceCompare.faceCompare(imgUtil.downLoadImgToBase64(teacher.getFaceUrl()), teacher.getFaceEncoder(), loginWithFaceDto.getImgBase64(), loginWithFaceDto.getImgEncoder());
            if (compare){
                user user = new user(teacher.getSchool(),teacher.getTeacher_id(),teacher.getName(),teacher.getPhone(),"1",teacher.getImg(),teacher.getDepartment(),teacher.getAcademic_title());
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("登录失败，对比不正确");
            }
        }

    }

    @Override
    public Result loginWithPhone(loginWithPhone loginWithPhone) {
        //先判断是否为学生
        student student = userMapper.selectStudentByPhone(loginWithPhone.getPhone());
        if (student!=null){
            String code = redisTemplate.opsForValue().get(loginWithPhone.getPhone() + "login");
            if (code == null) return Result.error("验证码过期");
            if (code.equals( loginWithPhone.getCode())){
                user user = new user(student.getSchool(),student.getStudent_id(),student.getName(),student.getPhone(),"0",student.getImg(),student.getDepartment(),"");
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("验证码错误");
            }
        }else {
            teacher teacher = userMapper.selectTeacherByPhone(loginWithPhone.getPhone());
            if (teacher == null) return Result.error("手机号未注册");
            String code = redisTemplate.opsForValue().get(loginWithPhone.getPhone() + "login");
            if (code == null) return Result.error("验证码过期");
            if (code.equals( loginWithPhone.getCode())){
                user user = new user(teacher.getSchool(),teacher.getTeacher_id(),teacher.getName(),teacher.getPhone(),"1",teacher.getImg(),teacher.getDepartment(),teacher.getAcademic_title());
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("验证码错误");
            }
        }
    }

    @Override
    public Result registerCheck(registerCheckDto registerCheckDto) {
        String code = redisTemplate.opsForValue().get(registerCheckDto.getPhone()+"register");
        if (code==null) return Result.error("验证码过期");
        if (code.equals(registerCheckDto.getCode())){
            return Result.success("验证成功");
        }else {
            return Result.error("验证码错误");
        }
    }

    @Override
    public Result<String> sendMessage(sendMessageDto sendMessageDto)  {
        Random random = new Random();
        StringBuilder codeBuilder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int digit = random.nextInt(10); // 生成0-9的随机数
            codeBuilder.append(digit);
        }
        String code = codeBuilder.toString();
        try {
            long expireTime = 60; // 例如，设置有效期为5分钟，单位是秒
            TimeUnit timeUnit = TimeUnit.SECONDS; // 设置时间单位
            redisTemplate.opsForValue().set(sendMessageDto.getPhone()+sendMessageDto.getType(),code,expireTime,timeUnit);
            String type="";
            if (sendMessageDto.getType().equals("register")){
                type= TemplateCode.register.getCode();
            }else if(sendMessageDto.getType().equals("login")){
                type=TemplateCode.login.getCode();
            }
            messageUtil.sendMessage(type,sendMessageDto.getPhone(),code);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("获取验证码失败");
        }

        return Result.success("获取验证码成功");
    }

    @Override
    public Result<String> teacherRegister(teacher teacher) {
        //验证手机号是否注册过
        com.ken.common.pojo.teacher Teacher = userMapper.selectTeacherByPhone(teacher.getPhone());
        student student = userMapper.selectStudentByPhone(teacher.getPhone());
        if(Teacher != null || student != null) return Result.error("手机号已注册");
        //将密码加密
        teacher.setPassword(DigestUtils.md5DigestAsHex(teacher.getPassword().getBytes()));
        //将用户写入数据库
        userMapper.teacherRegister(teacher);
        return Result.success("注册成功");
    }

    @Override
    public Result<String> studentRegister(student student) {
        //验证手机号是否注册过
        com.ken.common.pojo.teacher Teacher = userMapper.selectTeacherByPhone(student.getPhone());
        student Student = userMapper.selectStudentByPhone(student.getPhone());
        if(Student != null || Teacher != null) return Result.error("手机号已注册");
        //将密码加密
        student.setPassword(DigestUtils.md5DigestAsHex(student.getPassword().getBytes()));
        //将用户写入数据库
        userMapper.studentRegister(student);
        return Result.success("注册成功");
    }

    @Override
    public Result<String> loginWithPassword(loginWithPasswordDto login) {
        System.out.println(login);
        //如果是学生
        if (login.getRole().equals("0")){
            student student = userMapper.selectStudentBySchoolId(login.getId(),login.getSchool());
            System.out.println(student);
            if (student==null) return Result.error("用户不存在");
            log.info(DigestUtils.md5DigestAsHex(login.getId().getBytes()));
            if (DigestUtils.md5DigestAsHex(login.getId().getBytes()).equals(student.getPassword())){
                user user = new user(student.getSchool(),student.getStudent_id(),student.getName(),student.getPhone(),"0",student.getImg(),student.getDepartment(),"");
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("密码不正确");
            }
        }else if (login.getRole().equals("1")){
            teacher teacher = userMapper.selectTeachBySchoolId(login.getId(),login.getSchool());
            if (teacher==null) return Result.error("用户不存在");
            log.info(DigestUtils.md5DigestAsHex(login.getId().getBytes()));
            if (DigestUtils.md5DigestAsHex(login.getId().getBytes()).equals(teacher.getPassword())){
                user user = new user(teacher.getSchool(),teacher.getTeacher_id(),teacher.getName(),teacher.getPhone(),"1",teacher.getImg(),teacher.getDepartment(),teacher.getAcademic_title());
                String jwt = JWTUtil.createJWT(user);
                return Result.success(jwt);
            }else {
                return Result.error("密码不正确");
            }
        }
        return Result.error("不存在的用户角色");
    }

    @Override
    public Result<String> joinClass(joinClassDto dto) {
        //检查要加入的班级是否存在
        Class aClass = userMapper.selectClassById(dto.getClass_id());
        if (aClass==null) return Result.error("查找不到班级信息");
        //判定加入者是老师还是学生
        if (dto.getRole().equals("0")){
            if (userMapper.checkClassStudent(dto.getClass_id(),dto.getId())==null){
                userMapper.addStudent(dto.getClass_id(),dto.getId());
                //若是学生将班级学生数加一
                userMapper.increaseNum(dto);
                return Result.success("加入班级成功");
            }else {
                return Result.error("学生已在班级中");
            }

        }else {
            if (userMapper.checkClassTeacher(dto.getClass_id(),dto.getId())==null){
                userMapper.addTeacher(dto.getClass_id(),dto.getId());
                return Result.success("加入班级成功");
            }else {
                return Result.error("老师已在班级中");
            }

        }
    }

    @Override
    public Result getClassStudent(String class_id) {
        //判断class_id是否存在
        Class aClass = userMapper.selectClassById(class_id);
        if (aClass==null) return Result.error("班级不存在");
        List<student> classStudent = userMapper.getClassStudent(class_id);
        log.info(classStudent.toString());
        return Result.success(classStudent);
    }

    @Override
    public List<questionVo> getQuestionByHomework(String homework_id) {
        List<questionVo> questionByHomework = userMapper.getQuestionByHomework(homework_id);
        log.info(questionByHomework.toString());
        return questionByHomework;
    }

    @Override
    public List<homeworkVo> getHomeworkByClass(String class_id) {
        List<homeworkVo> homeworks = userMapper.getHomeworkByClass(class_id);
        for(homeworkVo homeworkVo :homeworks){
            //查找创建者
            homework homework = userMapper.getHomeworkById(homeworkVo.getHomework_id());
            homeworkVo.setTeacher_name(userMapper.selectTeacherById(homework.getTeacher_id()).getName());
            homeworkVo.setHomework_name(userMapper.getHomeworkById(homeworkVo.getHomework_id()).getHomework_name());
        }
        return homeworks;
    }

    @Override
    public List<examVo> getExamByClass(String class_id) {
        List<examVo> exams = userMapper.getExamByClass(class_id);
        return exams;
    }

    @Override
    public paperVo getPaperById(String paper_id) {
        paperVo paper = userMapper.getPaperById(paper_id);
        return paper;
    }

    @Override
    public List<questionVo> getQuestionByPaper(String paper_id) {

        return userMapper.getQuestionByPaper(paper_id);
    }

    @Override
    public List<answerVo> get_answerByHomework(String student_id, String homework_id) {
        //根据作业号获取作业中的题目的id
        List<String> questionId = userMapper.getQuestionIdByHomework(homework_id);
        ArrayList<answerVo> answerVos = new ArrayList<>();
        //根据作业id，题目id，学号获取回答情况
        for(String id:questionId){
            answerVo answer = userMapper.getAnswer(homework_id, student_id, id);
            answerVos.add(answer);
        }
        return answerVos;
    }

    @Override
    public homework_stateVo get_homeworkState(String student_id, String homework_id) {
        return userMapper.get_homeworkState(student_id, homework_id);
    }

    @Override
    public void save_wrongQuestion(wrongQuestionDto dto) {
        //使用三种id获取学生的id与题目的正确答案
        answerVo answer = userMapper.getAnswer(dto.getHomework_id(), dto.getStudent_id(), dto.getQuestion_id());
        Question question = userMapper.get_questionById(dto.getQuestion_id());
        dto.setCorrect_answer(question.getAnswer());
        dto.setStudent_answer(answer.getContent());
        userMapper.save_wrongQuestion(dto);
    }

    @Override
    public List<unitVo> getUnitByClass(String course_id) {
        return userMapper.getUnitByClass(course_id);
    }

    @Override
    public List<lessonVo> getLessonByUnit(String unit_id) {
        return userMapper.getLessonByUnit(unit_id);
    }

    @Override
    public long upload_studyRecord(studyRecordDto dto) {
        //计算总时长
        Duration time = Duration.between(dto.getStart_time(), dto.getEnd_time());
        dto.setTotal_time(time.getSeconds());
        //将学习记录上传到数据库保存
        userMapper.upload_studyRecord(dto);
        return time.getSeconds();
    }

    @Override
    public Class getClassByClassId(String class_id) {
        return userMapper.getClassByClassId(class_id);
    }

    @Override
    public List<Class> getClassByCourseId(String course_id) {
        return userMapper.getClassByCourseId(course_id);
    }

    @Override
    public String translation_word(MultipartFile multipartFile,String from,String to) throws Exception {
        File file = convertMultipartFileToFile(multipartFile);
        File document = WordDocumentUtils.copyDocument(file, from, to);
        String fileName = UUID.randomUUID().toString();
        String url = OssUtil.upload(document, fileName);
        return url;
    }

    @Override
    public commentaryVo commentary(MultipartFile file,String language,String change_language) throws Exception {
        String form = "cn";
        if (language.equals("en_vip")) form="en";
        File File = convertMultipartFileToFile(file);
        //将传入的音频先转换成文字
        String text = WebIATWS.SpeechToText(File,change_language);
        //将音频进行评分
        //File mp3File = AudioConverterUtil.convertToMP3(File);
        commentary commentary = IseDemo.speech_evaluation(File, text, language);
        commentaryVo commentaryVo = new commentaryVo();
        commentaryVo.setFluency_score(Double.parseDouble(commentary.getFluency_score()));
        commentaryVo.setPhone_score(Double.parseDouble(commentary.getPhone_score()));
        commentaryVo.setIntegrity_score(Double.parseDouble(commentary.getIntegrity_score()));
        commentaryVo.setTotal_score(Double.parseDouble(commentary.getTotal_score()));
        commentaryVo.setText(text);
        commentaryVo.setTranslation(WebITS.translation(text,form,"cn"));
        log.info("开始返回数据");
        return commentaryVo;
    }

    @Override
    public String SpeechToText(String text, String language) throws Exception {
        //将其生成为语音
        File conversation = WebTtsWs.conversation(text, language);

        //将其上传到oss
        String uuid = UUID.randomUUID().toString();
        return OssUtil.upload(conversation,uuid+".mp3");
    }

    @Override
    public Result<String> check_homework(check_homeworkDto dto) {
        log.info("检查班级"+dto.getClass_id()+"的作业"+dto.getHomework_id());
        //通过班级id和作业id获取过期和发布时间
        homeworkVo homeworkState = userMapper.getHomeworkState(dto);
        //检查作业的状态，若过期则更改状态，并返回已过期，否则返回过期时间
        LocalDateTime now = LocalDateTime.now();
        if (now.isAfter(homeworkState.getDue_date())){
            //将班级作业改为过期
            userMapper.changeClassHomeworkState(dto);
            return Result.error("作业已过期");
        }
        //计算离过期还有多少时间
        Duration between = Duration.between(now, homeworkState.getDue_date());
        long totalMinutes = between.toMinutes();
        long hours = totalMinutes / 60;
        long minutes = totalMinutes % 60;
        return Result.success("剩余"+hours+"小时"+minutes+"分钟");
    }

    @Override
    public List<homework> getHomeworkByCourseId(String course_id) {
        List<homework> homeworks = userMapper.getHomeworkByCourseId(course_id);
        for(homework homework:homeworks){
            teacher teacher = userMapper.selectTeacherById(homework.getTeacher_id());
            homework.setTeacher_name(teacher.getName());
        }
        return homeworks;
    }

    @Override
    public Question getQuestionById(String question_id) {
        return userMapper.get_questionById(question_id);
    }

    @Override
    public getMessageDto getMessageByClassId(String class_id) {
        getMessageDto getMessageDto = new getMessageDto();
        List<message> messages = userMapper.getMessageByClassId(class_id);
        for(message message:messages){
            if (message.getEnd_time().isBefore(LocalDateTime.now())){
                //设置为已结束
                getMessageDto.setEnd_num(getMessageDto.getEnd_num()+1);
                message.setIs_end("1");
            }else {
                //设置为未结束
                getMessageDto.setIng_num(getMessageDto.getIng_num()+1);
                message.setIs_end("0");
            }

        }
        getMessageDto.setMessages(messages);
        return getMessageDto;
    }

    @Override
    public homework getHomeworkContent(String homework_id) {
        homework homework = userMapper.getHomeworkById(homework_id);
        return homework;
    }

    @Override
    public List<attendance> getAttendanceRecordState(String code) {
        return userMapper.getAttendanceRecordState(code);
    }

    @Override
    public void change_messageState(String student_id, String message_id) {
        userMapper.change_messageState(student_id,message_id);
    }

    @Override
    public String upload_file(MultipartFile file, String extension) throws Exception {
        File tempFile = File.createTempFile("upload_", extension);
        file.transferTo(tempFile);
        String name = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 6)+"."+extension;
        String url = OssUtil.upload(tempFile, name);
        return url;
    }

    @Override
    public void create_knowledgePoints(knowledgePoint knowledgePoint) {
        if(userMapper.getKnowledgePointById(knowledgePoint.getId())==null){
            userMapper.create_knowledgePoints(knowledgePoint);
        }
    }

    @Override
    public List<knowledgePoint> getKnowledgePoint(String root) {
        ArrayList<knowledgePoint> knowledgePoints = new ArrayList<>();
        //先获取根节点自己
        knowledgePoint knowledgePoint = userMapper.select_knowledgePoint(root);
        knowledgePoints.add(knowledgePoint);
        //获取根节点的子节点
        select_knowledgePoint(root,knowledgePoints);
        return knowledgePoints;
    }

    @Override
    public void delete_knowledgePoints(List<String> roots) {
        for(String root : roots){
            knowledgePoint_delete(root);
        }
    }

    @Override
    public List<String> save_questions(List<Question> questionList) {
        ArrayList<String> strings = new ArrayList<>();
        for(Question question : questionList){
            String id = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 6);
            strings.add(id);
            question.setId(id);
            question.setCreated_at(LocalDateTime.now());
            question.setUpdated_at(LocalDateTime.now());
            userMapper.save_question(question);

        }
        return strings;
    }

    @Override
    public lessons getLessonById(String lessons_id) {
        return userMapper.getLessonById(lessons_id);
    }

    @Override
    public List<lessons> getLessonsByUnit(String unit_id) {
        return userMapper.getLessonsByUnit(unit_id);
    }

    @Override
    public void update_knowledgePoints(List<knowledgePoint> knowledgePoints) {
        for(knowledgePoint knowledgePoint:knowledgePoints){
            if (userMapper.getKnowledgePointById(knowledgePoint.getId())!=null){
                userMapper.create_knowledgePoints(knowledgePoint);
            }else {
                userMapper.delete_knowledgePoints(knowledgePoint.getId());
                userMapper.create_knowledgePoints(knowledgePoint);
            }
        }
    }

    @Override
    public knowledgePoint getKnowledgePointById(String knowledgePointId) {
        return userMapper.getKnowledgePointById(knowledgePointId);
    }

    @Override
    public List<studyRecord> getRecordById(String student_id) {
        return userMapper.getRecordById(student_id);
    }

    @Override
    public void update_knowledgePoint(knowledgePoint knowledgePoint) {
        userMapper.update_knowledgePoint(knowledgePoint);
    }

    @Override
    public void delete_lessonKnowledge(String lesson_id) {
        userMapper.delete_lessonKnowledge(lesson_id);
    }

    @Override
    public Long getLessonTotalTime(String student_id, String lesson_id) {
        return userMapper.getLessonTotalTime(student_id,lesson_id);
    }

    @Override
    public Result check_message(String message_id) {
        message message = userMapper.check_message(message_id);
        if (message.getEnd_time().isAfter(LocalDateTime.now())){
            Duration between = Duration.between(LocalDateTime.now(), message.getEnd_time());
            long totalMinutes = between.toMinutes();
            long hours = totalMinutes / 60;
            long minutes = totalMinutes % 60;
            long seconds = between.getSeconds() % 60; // 这里修正为直接获取剩余秒数
            return Result.success("剩余" + hours + "小时" + minutes + "分钟" + seconds + "秒");
        }
       return Result.error("已过期");
    }

    @Override
    public void update_user(update_userDto dto) {
        //学生处理
        if(dto.getRole().equals("0")){
            userMapper.update_student(dto);
            return;
        }
        //老师处理
        userMapper.update_teacher(dto);
    }

    @Override
    public void update_img(update_imgDto dto) throws Exception {
        log.info("信息"+dto.toString());
        //将64字节编码还原为图片并上传
        File file = imgUtil.Base64ToFileUtil(dto.getImgString(),".jpg");
        UUID uuid = UUID.randomUUID();
        String url = OssUtil.upload(file, uuid.toString());
        System.out.println(url);
        dto.setImg(url);
        //判断用户角色
        if (dto.getRole().equals("0")){
            userMapper.update_studentImg(dto);
        }else {
            userMapper.update_teacherImg(dto);
        }
    }

    @Override
    public user getUserInfo(String phone) {

        student student = userMapper.selectStudentByPhone(phone);
        if(student!=null){
            user user = new user(student.getSchool(),student.getStudent_id(),student.getName(),student.getPhone(),"0",student.getImg(),student.getDepartment(),"+");
            return user;
        }else {
            teacher teacher = userMapper.selectTeacherByPhone(phone);
            user user = new user(teacher.getSchool(),teacher.getTeacher_id(),teacher.getName(),teacher.getPhone(),"1",teacher.getImg(),teacher.getDepartment(),teacher.getAcademic_title());
            return user;
        }

    }

    @Override
    public List<course> getAllCourse() {
        return userMapper.getAllCourse();
    }

    @Override
    public String post_img(String imgBase64) throws Exception {
        //将base64编码转为文件
        File file = imgUtil.Base64ToFileUtil(imgBase64,"jpg");
        //将file上传
        UUID uuid = UUID.randomUUID();
        String url = OssUtil.upload(file, uuid.toString() + ".jpg");

        return url;
    }

    private File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        Path tempDir = Files.createTempDirectory("uploads");
        File tempFile = new File(tempDir.toFile(), multipartFile.getOriginalFilename());
        try (FileOutputStream fos = new FileOutputStream(tempFile)) {
            fos.write(multipartFile.getBytes());
        }
        return tempFile;
    }
    private void select_knowledgePoint(String root,ArrayList<knowledgePoint> knowledgePoints){
        List<knowledgePoint> knowledgePoints1 = userMapper.select_knowledgePoints(root);
        if (knowledgePoints1==null) return;
        for (knowledgePoint knowledgePoint:knowledgePoints1){
            knowledgePoints.add(knowledgePoint);
            select_knowledgePoint(knowledgePoint.getId(),knowledgePoints);
        }
    }
    private void knowledgePoint_delete(String root){
        List<knowledgePoint> knowledgePoints = userMapper.select_knowledgePoints(root);
        userMapper.delete_knowledgePoints(root);
        for(knowledgePoint knowledgePoint:knowledgePoints){
            userMapper.delete_knowledgePoints(knowledgePoint.getId());
        }
    }
}
